package com.zat.mes.andrmes.dofun.qualitytest.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.*
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-02-17 16:53
 * @description: 质检计划
 */
class QcPlanViewModel(application: Application) : AndroidViewModel(application) {

    private val qcPlanListData: MutableLiveData<MutableList<QcPlanBean>> = MutableLiveData()

    private val qcWorkRecordData: MutableLiveData<MutableList<QcRecordBean>> = MutableLiveData()

    private val parantTypeData: MutableLiveData<Map<String, MutableList<ParantTypeItem>>> =
        MutableLiveData()
    private val insertData: MutableLiveData<String> = MutableLiveData()

    private val qcDetailData: MutableLiveData<QcRecordHasBean> = MutableLiveData()

    private val qcDetailDataList: MutableLiveData<MutableList<QcRecordHasBean>> = MutableLiveData()
    private val qcWorkSuData: MutableLiveData<String> = MutableLiveData()

    fun getQcWorkSuData(): MutableLiveData<String> {
        return qcWorkSuData;
    }

    fun getQcPlanList(): MutableLiveData<MutableList<QcPlanBean>> {
        return qcPlanListData
    }

    fun getQcWorkRecordData(): MutableLiveData<MutableList<QcRecordBean>> {
        return qcWorkRecordData
    }

    fun getParantTypeData(): MutableLiveData<Map<String, MutableList<ParantTypeItem>>> {
        return parantTypeData
    }

    fun getInsertData(): MutableLiveData<String> {
        return insertData
    }

    fun getQcDetailData(): MutableLiveData<QcRecordHasBean> {
        return qcDetailData
    }

    fun getQcDetailDataLList(): MutableLiveData<MutableList<QcRecordHasBean>> {
        return qcDetailDataList
    }

    /**
     * 质检计划
     */
    fun loadQcPlanList(empId: Long, startTime: String) {

        RetrofitManager.apiService(ApiService::class.java)
            .getQcPlanList(empId, startTime)
            .enqueue(object : MesCallBack<BaseResponse<List<QcPlanBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<QcPlanBean>>>?,
                    response: Response<BaseResponse<List<QcPlanBean>>>?
                ) {
                    val result = response?.body()?.result
                    qcPlanListData.postValue(result as MutableList<QcPlanBean>?)
                }

            })

    }
    /**
     * 质检记录
     */
    fun loadQcWorkRecord(empId: Long, orderStyleId: Long, packId: Long,clothsqRcode:String?,clothsRFIDcode:String?) {

        RetrofitManager.apiService(ApiService::class.java)
            .getQcReCord(empId, packId, orderStyleId,clothsqRcode,clothsRFIDcode)
            .enqueue(object : MesCallBack<BaseResponse<List<QcRecordBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<QcRecordBean>>>?,
                    response: Response<BaseResponse<List<QcRecordBean>>>?
                ) {
                    val result = response?.body()?.result
                    qcWorkRecordData.postValue(result as MutableList<QcRecordBean>?)
                }

            })

    }
    /**
     * 类型列表
     */
    fun loadParantTypeList(typeCode: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getParentTypeCode(typeCode)
            .enqueue(object : MesCallBack<BaseResponse<List<ParantTypeItem>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<ParantTypeItem>>>?,
                    response: Response<BaseResponse<List<ParantTypeItem>>>?
                ) {
                    val items = response?.body()?.result
                    val map = HashMap<String, MutableList<ParantTypeItem>>()
                    if (items != null) {
                        map.put(typeCode, items as MutableList<ParantTypeItem>)
                    }
                    parantTypeData.postValue(map)
                }

            })

    }

    /**
     * 插入质检记录
     */
    fun insertQcRecord(param: String) {

        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, param)
        RetrofitManager.apiService(ApiService::class.java)
            .insertQcRecord(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            insertData.postValue(getApplication<Application>().getString(R.string.post_success))
                        } else {
                            insertData.postValue(response?.body()?.message)
                        }
                    } else {
                        insertData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                    }
                }

            })
    }

    /**
     * 查询质检详情
     */
    fun selectQcRecordById(qcId: Long) {
        RetrofitManager.apiService(ApiService::class.java)
            .getRecordById(qcId)
            .enqueue(object : MesCallBack<BaseResponse<QcRecordHasBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<QcRecordHasBean>>?,
                    response: Response<BaseResponse<QcRecordHasBean>>?
                ) {
                    qcDetailData.postValue(response?.body()?.result)
                }

            })
    }


    /**
     * 组长查询巡检记录
     */
    fun loadQcRecordByClassBoss(empId: Long, startTime: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getRecordByClassBoss(empId, startTime)
            .enqueue(object : MesCallBack<BaseResponse<List<QcRecordHasBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<QcRecordHasBean>>>?,
                    response: Response<BaseResponse<List<QcRecordHasBean>>>?
                ) {
                    if (response?.body()?.result != null) {
                        qcDetailDataList.postValue(response?.body()?.result as MutableList<QcRecordHasBean>)
                    }
                }

            })
    }

    /**
     * 组长查询巡检记录
     */
    fun loadQcRecordByUser(empId: Long, qcDate: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getRecordByUser(empId, qcDate)
            .enqueue(object : MesCallBack<BaseResponse<List<QcRecordHasBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<QcRecordHasBean>>>?,
                    response: Response<BaseResponse<List<QcRecordHasBean>>>?
                ) {
                    if (response?.body()?.result != null) {
                        qcDetailDataList.postValue(response?.body()?.result as MutableList<QcRecordHasBean>)
                    }
                }

            })
    }

    /**
     * 组长处理质检详情
     */

    fun upDateQcRecord(param: String) {
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, param)
        RetrofitManager.apiService(ApiService::class.java)
            .upDateQcRecord(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            insertData.postValue(getApplication<Application>().getString(R.string.post_success))
                        } else {
                            insertData.postValue(response?.body()?.message)
                        }
                    } else {
                        insertData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                    }
                }

            })
    }

    /**
     * 质检分派后 员工完成工作
     */

    fun workQcRecordSu(param: String) {
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, param)
        RetrofitManager.apiService(ApiService::class.java)
            .workQcSu(requestBody)
            .enqueue(object : MesCallBack<BaseResponse<String>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: Response<BaseResponse<String>>?
                ) {
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            qcWorkSuData.postValue(getApplication<Application>().getString(R.string.post_success))
                        } else {
                            qcWorkSuData.postValue(response?.body()?.message)
                        }
                    } else {
                        qcWorkSuData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                    }
                }

            })
    }

}